import json

import requests


# 步骤1：新建会话，得到conversation_id
url = "https://qianfan.baidubce.com/v2/app/conversation"

payload = json.dumps({
    "app_id": "6c31123d-6103-4e6d-a546-f8fce6555807"
})
headers = {
    'Content-Type': 'application/json',
    'X-Appbuilder-Authorization': 'Bearer bce-v3/ALTAK-WT80OnqVYqiuiXVWpaWYc/448dafad3a1809cdd625d6aeb7b337e73556999d'
}

response = requests.request("POST", url, headers=headers, data=payload)

# 从以上json字符串中提取conversation_id
data = json.loads(response.text)
conversation_id = data['conversation_id']


# 步骤2：发送请求，得到AppBuilder的结果
url = "https://qianfan.baidubce.com/v2/app/conversation/runs"

payload = json.dumps({
    "app_id": "6c31123d-6103-4e6d-a546-f8fce6555807",
    "query": input('请输入问题:'),
    "stream": False,
    "conversation_id": conversation_id
})
headers = {
    'Content-Type': 'application/json',
    'X-Appbuilder-Authorization': 'Bearer bce-v3/ALTAK-WT80OnqVYqiuiXVWpaWYc/448dafad3a1809cdd625d6aeb7b337e73556999d'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

# 从以上json字符串中提取conversation_id
data = json.loads(response.text)
conversation_id = data['answer']